import { createRouter, createWebHistory } from 'vue-router'
import Login from '../views/Login.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/', redirect: '/login' }, // 根路径重定向到登录页
    { path: '/login', name: 'login', component: Login, },
    { path: '/AGV/Station_list', name: 'Station_list', component: () => import('../views/AGV/Station_list.vue'), },
    { path: '/AGV/Station_add', name: 'Station_add', component: () => import('../views/AGV/Station_add.vue'), },
    { path: '/AGV/Station_edit/:id', name: 'Station_edit', component: () => import('../views/AGV/Station_add.vue'), },
    { path: '/AGV/AGV_map', name: 'AGV_map', component: () => import('../views/AGV/AGV_map.vue'), },
    { path: '/AGV/AGV_position', name: 'AGV_position', component: () => import('../views/AGV/AGV_position.vue'), },
    { path: '/AGV/AGV_car_list', name: 'AGV_car_list', component: () => import('../views/AGV/AGV_car_list.vue'), },
    { path: '/AGV/AGV_car_add', name: 'AGV_car_add', component: () => import('../views/AGV/AGV_car_add.vue'), },
    { path: '/AGV/AGV_car_edit/:id', name: 'AGV_car_edit', component: () => import('../views/AGV/AGV_car_add.vue'), },
    { path: '/AGV/AGV_simulation_list', name: 'AGV_simulation_list', component: () => import('../views/AGV/AGV_simulation_list.vue'), },
    { path: '/AGV/AGV_simulation_add', name: 'AGV_simulation_add', component: () => import('../views/AGV/AGV_simulation_add.vue'), },
    { path: '/Task/Task_list', name: 'Task_list', component: () => import('../views/Task/Task_list.vue'), },
    { path: '/running/anhui', name: 'anhui', component: () => import('../views/running/anhui.vue'), },
    { path: '/running/luosi', name: 'luosi', component: () => import('../views/running/luosi.vue'), },
    { path: '/order/order_list', name: 'order_list', component: () => import('../views/order/order_list.vue'), },
    { path: '/order/order_add', name: 'order_add', component: () => import('../views/order/order_add.vue'), },
    { path: '/order/order_edit/:id', name: 'order_edit', component: () => import('../views/order/order_add.vue'), },
    { path: '/AGV/AGV_setting', name: 'AGV_setting', component: () => import('../views/AGV/AGV_setting.vue'), },
    { path: '/AGV/Station_setting', name: 'Station_setting', component: () => import('../views/AGV/Station_setting.vue'), },
    { path: '/AGV/Edge_list', name: 'Edge_list', component: () => import('../views/AGV/Edge_list.vue'), },
  ],
})

// 添加路由守卫
router.beforeEach((to, from, next) => {
  // 从cookie中获取token
  const getCookie = (name) => {
    const nameEQ = `${name}=`;
    const cookies = document.cookie.split(";");
    for (let i = 0; i < cookies.length; i++) {
      let cookie = cookies[i];
      while (cookie.charAt(0) === " ") cookie = cookie.substring(1);
      if (cookie.indexOf(nameEQ) === 0) {
        return decodeURIComponent(cookie.substring(nameEQ.length));
      }
    }
    return null;
  };

  // 如果访问的是登录页，直接放行
  if (to.path === '/login') {
    next();
    return;
  }

  // 检查是否有token
  const token = getCookie('access_token');
  if (!token) {
    // 没有token，重定向到登录页
    next('/login');
    return;
  }

  // 有token，放行
  next();
});

export default router
